%%%-------------------------------------------------------------------
%%% @author mirahs(2892727493@qq.com)
%%% @doc
%%%     日志管理器
%%% @end
%%%-------------------------------------------------------------------
-module(sys_logger).

-export([
    echo/1
    ,echo/2

    ,debug/1
    ,debug/2
    ,debug/4
    ,info/1
    ,info/2
    ,info/4
    ,error/1
    ,error/2
    ,error/4
]).


%%%===================================================================
%%% API functions
%%%===================================================================

%% 输出信息到控制台
echo(Format) ->
    echo(Format, []).
echo(Format, Args) ->
    io:format("~ts~n", [io_lib:format(Format, Args)]).

%% 输出调试信息到控制台
debug(Format) ->
    debug(Format, []).
debug(Format, Args) ->
    debug(Format, Args, undefined, undefined).
debug(Format, Args, Mod, Line) ->
    Msg = format("debug", Format, Args, Mod, Line),
    io:format("~ts~n", [Msg]).

%% 输出重要信息到控制台
info(Msg) ->
    info(Msg, []).
info(Format, Args) ->
    info(Format, Args, null, null).
info(Format, Args, Module, Line) ->
    Msg = format("info", Format, Args, Module, Line),
    io:format("~ts~n", [Msg]).

%% 输出错误信息到控制台
error(Msg) ->
    ?MODULE:error(Msg, []).
error(Format, Args) ->
    ?MODULE:error(Format, Args, null, null).
error(Format, Args, Module, Line) ->
    Msg = format("error", Format, Args, Module, Line),
    io:format("~ts~n", [Msg]).


%%%===================================================================
%%% Internal functions
%%%===================================================================

format(T, F, A, Mod, Line) ->
    {{Y, M, D}, {H, I, S}} = erlang:localtime(),
    Date = util_io:fbin("~B/~2.10.0B/~2.10.0B ~2.10.0B:~2.10.0B:~2.10.0B", [Y,M,D,H,I,S]),

    case Line of
        undefined -> io_lib:format(lists:concat(["#app ", T, " ~s ", F]), [Date] ++ A);
        _ -> io_lib:format(lists:concat(["#app ", T, " ~s [~w:~w] ", F]), [Date, Mod, Line] ++ A)
    end.
